package com.zhuss.excecise.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.excecise.excecise.UpdateExceciseBo;
import com.zhuss.excecise.db.dao.ext.ExceciseDao;
import com.zhuss.excecise.db.entity.Excecise;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 创建练习生 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("exceciseUpdate")
public class ExceciseUpdate extends AbstractProcessor<Object, UpdateExceciseBo> implements Processor<Object, UpdateExceciseBo> {

    @Autowired
    private ExceciseDao exceciseDao;
    
    @Autowired
	@Qualifier("exceciseRead")
	private ExceciseRead exceciseRead;
    
    /**
     * 校验
     */
    @Override
    public void check(UpdateExceciseBo bo) throws WpsException {
    	Long exceciseId = bo.getExceciseId();
    	String title = bo.getTitle();
    	if (exceciseId == null || exceciseId <= 0l) {
    		throw new WpsException(ExceptionCode.练习生ID为空);
    	}
    	if (title == null || "".equals(title.trim())) {
    		throw new WpsException(ExceptionCode.练习生标题为空);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateExceciseBo param) {
    	Excecise record = new Excecise();
    	record.setExceciseId(param.getExceciseId());
    	record.setTitle(param.getTitle());
    	record.setContent(param.getContent());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	exceciseDao.updateByPrimaryKeySelective(record);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateExceciseBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.练习生.key());
    	record.setAction(LogActionEnum.练习生修改.key());
    	record.setTargetId(param.getExceciseId());
    	record.setTargetName(param.getTitle());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
